package tech.heptagram.staff.cas.service.staff.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import tech.heptagram.staff.cas.common.AssertUtils;
import tech.heptagram.staff.cas.domain.po.DataAuth;
import tech.heptagram.staff.cas.domain.po.Role;
import tech.heptagram.staff.cas.mapper.CasDataAuthMapper;
import tech.heptagram.staff.cas.service.staff.CasDataAuthService;
import tech.heptagram.staff.cas.service.staff.CasRoleService;

/**
 * @author wang.yuchuan
 * @createdAt 2019/1/11
 */
@Slf4j
@Service
public class CasDataAuthServiceImpl implements CasDataAuthService {

    @Resource
    private CasDataAuthMapper casDataAuthMapper;

    @Resource
    private CasRoleService casRoleService;

    @Override
    public List<DataAuth> findAll() {
        return casDataAuthMapper.selectAll();
    }

    @Override
    public void save(DataAuth dataAuth) {
        AssertUtils.notNull(dataAuth, "数据权限对象不能为空");
        AssertUtils.hasText(dataAuth.getCreatedBy(), "创建人不能为空");
        AssertUtils.hasText(dataAuth.getName(), "数据权限名称不能为空");
        AssertUtils.hasText(dataAuth.getCode(), "数据权限编码不能为空");
        if(StringUtils.isEmpty(dataAuth.getDesc())) {
            dataAuth.setDesc("");
        }
        if(StringUtils.isEmpty(dataAuth.getModifiedBy())) {
            dataAuth.setModifiedBy(dataAuth.getCreatedBy());
        }

        casDataAuthMapper.insert(dataAuth);
    }

    @Override
    public void update(DataAuth dataAuth) {
        AssertUtils.notNull(dataAuth, "数据权限对象不能为空");
        AssertUtils.hasText(dataAuth.getModifiedBy(), "修改人不能为空");

        casDataAuthMapper.update(dataAuth);
    }

    @Override
    public DataAuth findById(Integer id) {
        if(Objects.isNull(id)) {
            return null;
        }
        return casDataAuthMapper.selectById(id);
    }

    @Override
    public DataAuth findByRoleId(Integer roleId) {
        if(Objects.isNull(roleId)) {
            return null;
        }
        return casDataAuthMapper.selectByRoleId(roleId);
    }

    @Override
    public List<DataAuth> findByRoleIds(List<Integer> roleIds) {
        if(CollectionUtils.isEmpty(roleIds)) {
            return new ArrayList<>();
        }

        List<DataAuth> dataAuths = casDataAuthMapper.selectByRoleIds(roleIds);

        // 去重
        List<DataAuth> result = new ArrayList<>();

        for (DataAuth dataAuth : dataAuths) {
            Optional<DataAuth> dataAuthOptional = result.stream().filter(da -> da.getId().intValue() == dataAuth.getId()).findAny();
            if(!dataAuthOptional.isPresent()) {
                result.add(dataAuth);
            }
        }
        return result;
    }

    @Override
    public List<DataAuth> findByStaffCode(String staffCode) {
        if(StringUtils.isEmpty(staffCode)) {
            return new ArrayList<>();
        }

        // 获取员工的所有角色Ids, 如果没有设置任何角色, 就没有操作权限
        List<Role> roles = casRoleService.findRolesByStaffCode(staffCode);
        List<Integer> lstRoleIds = roles.stream().map(Role::getId).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(lstRoleIds)){
            return new ArrayList<>();
        }


        List<DataAuth> dataAuths = findByRoleIds(lstRoleIds);
        return dataAuths;
    }
}
